package com.hcx.poi.hssf.eventmodel;

import java.math.BigDecimal;

import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.CellRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.SSTRecord;

import com.hcx.excel.xlsx.Cell;

public class CellRecordHandler {
	private SSTRecord sstrec;

	public CellRecordHandler(SSTRecord sstrec) {
		this.sstrec = sstrec;
	}

	public Cell handle(CellRecord record) {
		switch (record.getSid()) {
		case NumberRecord.sid:
			return handleNumberRecord((NumberRecord) record);
		case LabelSSTRecord.sid:
			return handleLabelSSTRecord((LabelSSTRecord) record);
		case BoolErrRecord.sid:
			break;
		case FormulaRecord.sid:
			return handleFormulaRecord((FormulaRecord) record);
		case RKRecord.sid:
			break;
		}
		return null;
	}

	private Cell handleNumberRecord(NumberRecord record) {
		Cell cell = new Cell();
		cell.setValue(new BigDecimal(record.getValue()).toString());
		return cell;
	}

	private Cell handleLabelSSTRecord(LabelSSTRecord record) {
		Cell cell = new Cell();
		cell.setValue(sstrec.getString(record.getSSTIndex()).toString());
		return cell;
	}

	private Cell handleFormulaRecord(FormulaRecord record) {
		Cell cell = new Cell();
		cell.setValue(new BigDecimal(record.getValue()).toString());
		return cell;
	}
}
